package com.example.transaction.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.transaction.common.vo.Result;
import com.example.transaction.sys.entity.Item;
import com.example.transaction.sys.entity.ItemClassfy;
import com.example.transaction.sys.entity.LoginLog;
import com.example.transaction.sys.service.IItemClassfyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zh
 * @since 2023-06-26
 */
@RestController
@RequestMapping("/sys/itemClassfy")
public class ItemClassfyController {

    @Autowired
    private IItemClassfyService itemClassfyService;

    @GetMapping("/all")
    public Result<List<ItemClassfy>> getAllItemClassfy(){
        List<ItemClassfy>list=itemClassfyService.list();
        return Result.success(list,"查询成功");
    }

    /**
     * 新增商品对应类别接口
     * @param itemClassfy
     * @return
     */
    @PostMapping("/addItemClassfy")
    public Result<?>addItemClassfy(@RequestBody ItemClassfy itemClassfy){
        try {
            itemClassfyService.save(itemClassfy);
        }catch (Exception e){
            return Result.fail("新增失败");
        }
        return Result.success("新增商品对应类别成功");
    }

    /**
     * 根据物品id查找所有类别
     * @param itemID
     * @return
     */
    @GetMapping("/itemID/{itemID}")
    public Result<List<ItemClassfy>>getItemClassfyByItemId(@PathVariable("itemID")String itemID){
        LambdaQueryWrapper<ItemClassfy> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(itemID),ItemClassfy::getItemID,itemID);
        List<ItemClassfy> itemClassfy=itemClassfyService.list(wrapper);
        return Result.success(itemClassfy);
    }

    /**
     * 根据类别id查找所有物品id
     * @param classfyID
     * @return
     */
    @GetMapping("/classfyID/{classfyID}")
    public Result<List<ItemClassfy>>getItemClassfyByClassfyId(@PathVariable("classfyID")String classfyID){
        LambdaQueryWrapper<ItemClassfy> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(classfyID),ItemClassfy::getClassfyID,classfyID);
        List<ItemClassfy> itemClassfy=itemClassfyService.list(wrapper);
        return Result.success(itemClassfy);
    }

    /**
     * 根据itemID和classfyID更改classfyID
     * @param
     * @return
     */
    @PutMapping("/updateItemClassfy")
    public Result<?>updateItemClassfy(@RequestBody ItemClassfy itemClassfy){
        try {
            itemClassfyService.updateById(itemClassfy);
        }catch (Exception e){
            return Result.fail("修改失败");
        }
        return Result.success("修改商品类别成功");
    }


}
